package com.lys.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.commonutils.R;
import com.lys.eduservice.entity.EduTeacher;
import com.lys.eduservice.entity.vo.TeacherQuery;
import com.lys.eduservice.service.EduTeacherService;
import com.lys.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lys
 * @since 2022-06-24
 */
@RestController
@Api(tags = "讲师管理接口")
@Slf4j
@RequestMapping("/eduservice/eduTeacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    //查询所有teacher
    @GetMapping("findAll")
    @ApiOperation(value = "查询所有teacher")
    public R findAll(){
        List<EduTeacher> eduTeacherList= eduTeacherService.list(null);
        return R.ok().data("items",eduTeacherList);

    }

    //逻辑删除
    @DeleteMapping("{id}")
    @ApiOperation(value = "逻辑删除")
    public R deleteById(@ApiParam(name = "id",value = "讲师id",required = true) @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //分页查询
    @GetMapping("/pageTeacher/{page}/{pageSize}")
    @ApiOperation(value = "分页查询讲师")
    public R pageTeacher(@PathVariable long page,@PathVariable long pageSize){
//        try{
//            int i = 10/0;
//        }catch (Exception e){
//            throw new GuliException(20001,"执行了自定义异常处理..");
//        }

        //分页构造器
        Page<EduTeacher> pageInfo = new Page<>(page,pageSize);

        eduTeacherService.page(pageInfo,null);

        return  R.ok().data("ietms",pageInfo);
    }

    @PostMapping("pageTeacherByCondition/{page}/{pageSize}")
    @ApiOperation(value = "根据条件分页查询讲师")
    public R pageTeacherByCondition(@PathVariable long page, @PathVariable long pageSize,@RequestBody(required = false) TeacherQuery teacherQuery){
        //获取条件参数
        log.info(teacherQuery.getName());
        String name = teacherQuery.getName();
        String begin =teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        //分页构造器
        Page<EduTeacher> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,EduTeacher::getName,name);
        queryWrapper.eq(level!=null,EduTeacher::getLevel,level);
        queryWrapper.ge(begin!=null,EduTeacher::getGmtCreate,begin);
        queryWrapper.le(end!=null,EduTeacher::getGmtModified,end);
        queryWrapper.orderByAsc(EduTeacher::getSort);

        eduTeacherService.page(pageInfo,queryWrapper);
        Long total = pageInfo.getTotal();
        List<EduTeacher> eduTeacherList = pageInfo.getRecords();

        return  R.ok().data("total",total).data("eduTeacherList",eduTeacherList);
    }

    //添加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //根据id查询
    @GetMapping("getTeacher/{id}")
    @ApiOperation("根据id查询")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher= eduTeacherService.getById(id);
        return R.ok().data("items",eduTeacher);
    }
    //修改讲师
    @PostMapping("updateTeacher")
    @ApiOperation("修改讲师")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag =eduTeacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

